package com.dins.elashutina.parser;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * User: ekaterina.lashutina
 * Date: 28.11.13
 * Time: 15:00
 */
public class JaCoCoParser {

    static String path = "\\coverage";
    static Stack<String> stack = new Stack<String>();
    static double[] val = new double[6];

    public static Tree startJaCoCoParser(File fileName, Tree tree) {
        try {
            stack.push(path);
            tree.add(new Node(path,path,null, null));
            BufferedReader reader = new BufferedReader(new FileReader(fileName));
            String line;
            while ((line = reader.readLine()) != null) {
                line = line.replaceAll("\t", "");
                jacocoParse(line, tree);
            }
            return tree;
        } catch (FileNotFoundException e) {
            System.out.println("File not found" + fileName);
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println("Exception");
            e.printStackTrace();
        }
        return tree;
    }

    public static void jacocoParse(String line, Tree tree) {
        List<String> args = new ArrayList<String>();
        String str;
        if (line.startsWith("<package ") || line.startsWith("<class ") || line.startsWith("<method ") || line.startsWith("<sourcefile")) {
            String[] tmp = line.split("\"");
            String newpath = "";
            newpath += "\\" + tmp[1];
            if (line.startsWith("<package ")) {
                stack.push(path + newpath);
            }
            if (line.startsWith("<class ")) {
                stack.push(path + newpath);
            }
            if (line.startsWith("<sourcefile ")) {
                stack.push(path + newpath);
            }
            if (line.startsWith("<method ")) {
                newpath += tmp[3];
                stack.push(path + newpath);
            }
            tree.add(new Node(newpath, path + newpath, null, tree.getNode(path)));
            path += newpath;
        }
        if (line.startsWith("<counter ")) {
            String[] tmp = line.split("\"");
            String newpath = "";
            newpath += "" + tmp[1];
            if (newpath.equals("INSTRUCTION")) {
                System.out.println(Double.parseDouble(tmp[5]) / (Double.parseDouble(tmp[3]) + Double.parseDouble(tmp[5])));
                val[0] = Double.parseDouble(tmp[5]) / (Double.parseDouble(tmp[3]) + Double.parseDouble(tmp[5]));
            }
            if (newpath.equals("BRANCH")) {
                val[1] = Double.parseDouble(tmp[5]) / (Double.parseDouble(tmp[3]) + Double.parseDouble(tmp[5]));
            }
            if (newpath.equals("LINE")) {
                val[2] = Double.parseDouble(tmp[5]) / (Double.parseDouble(tmp[3]) + Double.parseDouble(tmp[5]));
            }
            if (newpath.equals("COMPLEXITY")) {
                val[3] = Double.parseDouble(tmp[5]) / (Double.parseDouble(tmp[3]) + Double.parseDouble(tmp[5]));
            }
            if (newpath.equals("METHOD")) {
                val[4] = Double.parseDouble(tmp[5]) / (Double.parseDouble(tmp[3]) + Double.parseDouble(tmp[5]));
            }
            if (newpath.equals("CLASS")) {
                val[5] = Double.parseDouble(tmp[5]) / (Double.parseDouble(tmp[3]) + Double.parseDouble(tmp[5]));
            }

        } for (int i = 0; i < val.length; i++) {
            args.add("" + val[i]);
        }
         if (line.startsWith("</package>") || line.startsWith("</class>") || line.startsWith("</method>") || line.startsWith("</sourcefile")) {
            str = stack.pop();
            tree.getNode(str).setValues(args);
            val = new double[6];
            String[] tmp = path.split("\\\\");
            path = "";
            for (int i = 1; i < tmp.length - 1; i++) {
                path += "\\" + tmp[i];
            }
        }
        if(line.startsWith("</report")){
            str = stack.pop();
            tree.getNode(str).setValues(args);
        }
    }
}
